/**
 * Created by Dylan on 2017/4/24.
 */
var TrendsType;
if (!TrendsType) {
    TrendsType = {}
}

TrendsType.operateFormatter = function (value, row, index) {
    return [
        '<a class="edit" href="javascript:void(0)" title="编辑"><i class="glyphicon glyphicon-pencil"></i></a>'
        // '&nbsp;&nbsp;&nbsp;&nbsp;' +
        // '<a class="remove" href="javascript:void(0)" title="删除"><i class="fa fa-trash-o"></i></a>'
    ].join('');
}

window.operateEvents = {
    'click .edit': function (e, value, row, index) {
        showEditTrendsType(row);
    },
    'click .remove': function (e, value, row, index) {
        comment.showOperationTips("确定删除吗？", function () {
            comment.post('/web/trendstype/deleteTrendsType', {
                id: row.id
            }, null, function (data) {
                if (data.res.code == 40000) {
                    // hint("删除成功");
                    comment.refresh();
                } else {
                    hint(data.res.msg);
                }
            });
        });
    }
}

var showEditTrendsType = function (row) {
    showModel("editTrendsType-div", function () {
        $("#editId").val(row.moduleId);
        $("#editTypeName").val(row.content);
        $("#editDescription").val(row.description);
        $("#editMoney").val(row.money);
    });
}

bindClick("button[data-event=submit-edit]", function () {
    var editTypeName = $("#editTypeName").val();
    if (editTypeName == null || editTypeName == '') {
        hint("互动类别名称不能为空");
        return false;
    }
    var div = $("#mergeTrendsType-div");
    var form = $("#mergeTrendsType");
    if (!form.valid) {
        return false;
    }
    var editTypeName = $("#editTypeName").val();
    var editDescription = $("#editDescription").val();
    var editMoney = $("#editMoney").val();
    var id = $("#editId").val();
    var param = {"moduleId": id, "content": editTypeName, "description": editDescription, "money": editMoney};
    $.ajax({
        type: "post",
        url: form.attr("action"),
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify(param),
        success:function (data) {
            if (data.res.code == 40000) {
                if (data.res.data = true) {
                    hint("修改成功");
                    closeModel("editTrendsType-div");
                    comment.refresh();
                } else {
                    hint("修改失败");
                }
            } else {
                hint(data.res.msg);
            }
        }
    })
});

bindClick("#addTrendsType", function () {
    showAddTrendsType();
});

var showAddTrendsType = function () {
    showModel("addTrendsType-div", function () {
        $("#addTypeName").val('');
    });
}

bindClick("button[data-event=submit-add]", function () {
    var addTypeName = $("#addTypeName").val();
    if (addTypeName == null || addTypeName == '') {
        hint("互动类别名称不能为空");
        return false;
    }
    var form = $("#addTrensTypeForm");
    if (!form.valid) {
        return false;
    }
    var param = form.formToArray();
    $.post(form.attr("action"), param, function (data) {
        if (data.res.code == 40000) {
            if (data.res.data = true) {
                hint("添加成功");
                closeModel("addTrendsType-div");
                comment.refresh();
            } else {
                hint("添加失败");
            }
        } else {
            hint(data.res.msg);
        }
    });
});

